package com.sbtr.controller;


import com.sbtr.business.auth.service.IRoleService;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.open.auth.dto.RoleAuthDto;
import com.sbtr.open.auth.dto.RoleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/auth/role")
@ApiIgnore
@Api(tags = "角色管理（暂未启用）")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找", notes = "根据id查找", httpMethod = "GET")
    public Response<RoleDto> findById(@PathVariable("id") Long id) {
        return Response.success(roleService.findById(id));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据id删除", notes = "根据id删除", httpMethod = "DELETE")
    public Response<Boolean> deleteById(@PathVariable("id") Long id) {
        boolean b = roleService.deleteById(id);
        if (b) {
            return Response.success("删除角色信息成功", b);
        } else {
            return Response.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败");
        }
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "GET")
    public Response<PageDto<RoleDto>> search(@RequestParam(name = "roleName", required = false) String roleName,
                                             @RequestParam("page") Long currPage, @RequestParam("pageSize") Long pageSize) {
        return Response.success(roleService.search(roleName, currPage, pageSize));
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改", notes = "新增或修改", httpMethod = "POST")
    public Response<RoleDto> save(@RequestBody RoleDto dto) {
        if (dto.getId() == null) {
//			dto.setCreater(UserUtils.getFdLoginName());
            dto.setCreateTime(new Date());
        }
        return Response.success("保存角色信息成功", roleService.save(dto));
    }

    @RequestMapping(value = "/{id}/auth", method = RequestMethod.POST)
    @ApiOperation(value = "添加授权", notes = "添加授权", httpMethod = "POST")
    public Response<Boolean> addAuth(@PathVariable("id") Long roleId, @RequestBody List<String> userIds) {
        return Response.success("添加授权成功", roleService.addAuth(roleId, userIds));
    }

    @RequestMapping(value = "/{id}/auth", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除授权", notes = "删除授权", httpMethod = "DELETE")
    public Response<Boolean> deleteAuth(@PathVariable("id") Long roleId, @RequestBody List<String> userIds) {
        return Response.success("删除授权成功", roleService.deleteAuth(roleId, userIds));
    }

    @RequestMapping(value = "/{id}/auth", method = RequestMethod.GET)
    @ApiOperation(value = "查询授权列表", notes = "查询授权列表", httpMethod = "GET")
    public Response<List<RoleAuthDto>> getAuth(@PathVariable("id") Long roleId) {
        return Response.success(roleService.getAuth(roleId));
    }

//	@RequestMapping(value = "/is-admin", method = RequestMethod.GET)
//	@ApiOperation(value = "是否管理员", notes = "是否管理员", httpMethod = "GET")
//	public Response<Boolean> isAdmin(){
//		boolean b = roleService.isAdmin(UserUtils.getFdLoginName());
//		return Response.success(b);
//	}

}

